// pages/publish/publish.js
import { request } from '../../request/index'
import regeneratorRuntime from '../../lib/runtime/runtime'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgList: [],
        modalName: null,
        textareaAValue: '',
    },

    
    /**
     * request POST 请求
     *  发布 Moment
     */
    publishMoment() {
        let imgListString = this.data.imgList.toString()
        console.log(imgListString)
        console.log(this.data.textareaAValue)
        wx.request({
          url: 'http://127.0.0.1:8000/SimondeCapAPI/publishMoment',
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          method: "POST",
          // data: { content: "xxx", imgURL: "[]" }
          data: {
              content: this.data.textareaAValue,
              imgList: imgListString
          },
          success: function(res) {
              console.log(res.data)
              if (res.data.data == "Succeed!") {
                wx.navigateBack({
                  delta: 1,     // 返回上一个页面
                })
                wx.showToast({
                  title: 'Publish Succeed!',
                  icon: 'success',
                  duration: 2000
                })
              } 
          }
        })
    },

    ChooseImage() {
        wx.chooseImage({
          count: 9, //默认9
          sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
          sourceType: ['album'], //从相册选择
          success: (res) => {
            if (this.data.imgList.length != 0) {
              this.setData({
                imgList: this.data.imgList.concat(res.tempFilePaths)
              })
            } else {
              this.setData({
                imgList: res.tempFilePaths
              })
            }
          }
        });
      },
      ViewImage(e) {
        wx.previewImage({
          urls: this.data.imgList,
          current: e.currentTarget.dataset.url
        });
      },
      DelImg(e) {
        wx.showModal({
          title: 'Hi Bro!',
          content: 'U Sure to Delete this?',
          cancelText: 'No',
          confirmText: 'Sure',
          success: res => {
            if (res.confirm) {
              this.data.imgList.splice(e.currentTarget.dataset.index, 1);
              console.log(this.data.imgList)
              this.setData({
                imgList: this.data.imgList
              })
            }
          }
        })
      },
      textareaAInput(e) {
        this.setData({
          textareaAValue: e.detail.value
        })
      },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})